package modes

import (
	"fmt"
	"time"
)

/*意见反馈表*/
type FeedBack struct {
	Id    int64    `json:"feed_id" xorm:"id"`       //意见反馈id
	UId   int64    `json:"feed_uid" xorm:"uid"`     //学员ID
	FType string   `json:"feed_type" xorm:"f_type"` //意见类型
	Title string   `json:"feed_title" xorm:"title"` //意见标题
	Descs string   `json:"feed_descs" xorm:"descs"` //问题描述
	At    int64    `json:"top_at" xorm:"at"`        //创建时间
	IsNew int64    `json:"is_new" xorm:"is_new"`    //是否为新版【1新 2旧】
	Urls  []string `json:"urls" xorm:"urls"`        // 图片的url

}

func (this *FeedBack) TableName() string {
	return "feedback"
}
func (this *FeedBack) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *FeedBack) Get() (bool, error) {
	return Db(0).Get(this)
}
func (this *FeedBack) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *FeedBack) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *FeedBack) Count(where string) (int64, error) {
	var val FeedBack
	return Db(0).Where(where).Count(&val)
}
func (this *FeedBack) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *FeedBack) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *FeedBack) where(fage, count, page int, where, field string) ([]FeedBack, error) {
	list := make([]FeedBack, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *FeedBack) List(val *Pager) error {
	val.TName = this.TableName()
	w := fmt.Sprintf("uid = %d ", this.UId)
	val.Total, _ = this.Count(w)
	list, err := this.where(val.Rules, val.Count, val.Page-1, w, "")
	if err == nil {
		val.List = list
	}
	return err
}

type FeedLook struct {
	FeedBack    `json:"feed" xorm:"extends"`
	StudentInfo `json:"usinfo" xorm:"extends"`
}

func (this *FeedLook) Lookups(val *Pager) error {
	list := make([]FeedLook, 0)
	val.TName = "feedback"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) //时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	//fmt.Println(val.Count,val.Page)
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Join("LEFT", "studen_info", fmt.Sprintf("%s.uid = studen_info.id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}

func (this *FeedBack) MyFeedBackList() ([]FeedBack, error) {
	w := fmt.Sprintf("uid = %d and  is_new=1 ", this.UId)
	list := make([]FeedBack, 0)
	err := Db(0).Where(w).
		Desc("at").Find(&list)
	return list, err
}
